<?php

class Soul_Model_Collection extends Soul_Collection_List {

    private $_itemClass= NULL;
	
    public function __construct() {
        parent::__construct();
    }
    
    public function setItemClass($value){
    	$this->_itemClass = $value;
    	return $this;
    }

    public function getItemClass(){
    	return $this->_itemClass;
    }
    
    public function newItem(){
    	$class = $this->getItemClass();
    	$result = Soul_Loader::loadInstance($class);
    	$this->append($result);
    	return $result;
    }

    public function hasItem($entity) {
        foreach ($this as $item) {
            if ($item->equal($entity)) {
                return TRUE;
            }
        }
        return FALSE;
    }

    public function getItemById($id) {
        foreach ($this as $item) {
            if ($item->equalId($id)){
                return $item;
            }
        }
        return NULL;
    }
    
    public function setItemPropertyValue($propertyName, $value) {
        foreach ($this as $item) {
            $item[$propertyName] = $value;
        }
        return $this;
    }

    public function getItemPropertyValue($propertyName) {
        $result = array();
        foreach ($this as $item) {
            $result[] = $item[$propertyName];
        }
        return $result;
    }    

    public function removeItemById($id) {
        $ids = Soul_Util_Array::pack($id);
        foreach ($ids as $itemId) {
            for ($i = 0; $i < count($this); $i++) {
                $item = $this[$i];
                if($item instanceof Soul_Model_Entity_Version){
                    if (strcmp($item->getVersionIdValue(), $itemId) == 0) {
                        $this->offsetUnset($i);
                        break;
                    }                    
                }else{
                    if (strcmp($item->getIdValue(), $itemId) == 0) {
                        $this->offsetUnset($i);
                        break;
                    }
                }
            }
        }
        sort($this->source);
        return $this;
    }
    
    public function fill($data){
        if(!is_null($data)){
	    	foreach($data as $row){
                $this->newItem()->fill($row);
	    	}
    	}
    	return $this;
    }
}